package com.sdp.right.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.springframework.util.StringUtils;

public class LocalDateUtil {

	/**
	 * 字符串转LocalDateTime
	 * @param d
	 * @param formatter
	 * @return
	 */
	public static LocalDateTime String2LocalDateTime(String d,String formatter) {
		if(StringUtils.isEmpty(formatter)) {
			return String2LocalDateTime(d);
		}
		DateTimeFormatter df = DateTimeFormatter.ofPattern(formatter);
		return LocalDateTime.parse(d, df);
	}
	
	public static LocalDateTime String2LocalDateTime(String d) {
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		return LocalDateTime.parse(d, df);
	}
	
	/**
	 * 字符串转LocalDate
	 * @param d
	 * @param formatter
	 * @return
	 */
	public static LocalDate String2LocalDate(String d,String formatter) {
		if(StringUtils.isEmpty(formatter)) {
			return String2LocalDate(d);
		}
		DateTimeFormatter df = DateTimeFormatter.ofPattern(formatter);
		return LocalDate.parse(d, df);
	}
	
	public static LocalDate String2LocalDate(String d) {
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		return LocalDate.parse(d, df);
	}
	
	public static String localDate2String(LocalDate d) {
		return DateTimeFormatter.ofPattern("yyyy-MM-dd").format(d);
	}
	
	public static String localDate2String(LocalDate d,String formatter) {
		if(StringUtils.isEmpty(formatter)) {
			return localDate2String(d);
		}
		return DateTimeFormatter.ofPattern(formatter).format(d);
	}
	
	public static String localDateTime2String(LocalDateTime d) {
		return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(d);
	}
	
	public static String localDateTime2String(LocalDateTime d,String formatter) {
		if(StringUtils.isEmpty(formatter)) {
			return localDateTime2String(d);
		}
		return DateTimeFormatter.ofPattern(formatter).format(d);
	}
}
